"use strict";

/**
 * @ngdoc function
 * @name jgappApp.controller:AttendanceWifilistCtrl
 * @description
 * # AttendanceWifilistCtrl
 * Controller of the jgappApp
 */
angular
  .module("jgappApp")
  .controller("AttendanceWifilistCtrl", function(Util, localStorageService) {
    var vm = this;
    var attDetail = localStorageService.get("attDetail") || {};
    var init = function() {
      vm.selectWifis = [];
      // 根据map组成数组，提交时候再弄成map
      vm.selectWifis = Object.keys(attDetail.wifiConfigMap || {}).map(function(
        mac
      ) {
        return {
          mac: mac,
          name: attDetail.wifiConfigMap[mac]
        };
      });
      // 将周围wifi与选择的做匹配
      vm.mergescanWifi();
    };
    this.mergescanWifi=function(){
      vm.scanWifis = NativeUtil.use("getScanResults") || [];
      // 将扫描的数据中已连接的数据踢出去
      vm.scanWifis = vm.scanWifis.filter(function(item) {
        return !vm.selectWifis.some(function(selItem) {
          return selItem.mac === item.mac;
        });
      });
    };
    this.addWifiConfirm = function(wifi) {
      navigator.notification.confirm(
        "是否确定为打卡wifi",
        vm.addWifi.bind(vm, wifi),
        "提示",
        ["确定", "取消"]
      );
    };
    this.addWifi = function(wifi) {
      vm.selectWifis.push(wifi);
      vm.mergescanWifi();
    };
    this.remWifiConfirm = function(wifi) {
      navigator.notification.confirm(
        "是否确定取消当前wifi为打卡wifi",
        vm.removeWifi.bind(vm, wifi),
        "提示",
        ["确定", "取消"]
      );
    };
    this.removeWifi = function(wifi) {
      vm.selectWifis = vm.selectWifis.filter(function(item) {
        return item.mac != wifi.mac;
      });
      vm.mergescanWifi();
    };
    this.confirm = function() {
      var wifiConfigMap = {};
      vm.selectWifis.forEach(function(item) {
        wifiConfigMap[item.mac] = item.name;
      });
      attDetail.wifiConfigMap = wifiConfigMap;
      localStorageService.set("attDetail", attDetail);
      Util.goBack();
    };
    init();
  });
